import { useState } from 'react';
import { useRequest, history } from 'umi';
import { Toast } from 'antd-mobile';
import service from '@/services';
import classNames from 'classnames';
import styles from './index.less';
import UseComponents from '@/components/useComponents';

export interface Info {
  fans_token: string;
  http_request_type: number;
}

const Home: React.FC = () => {
  const TYPE = 1;
  const TOKEN = '7573893fr38ovdem51q8vkrqjb5t8l24';
  const info = { http_request_type: TYPE, fans_token: TOKEN };
  const [message, setMessage] = useState('');

  const { run } = useRequest(service.useService.getUserInfo, {
    manual: true,
    onSuccess: (res) => {
      if (res.rt === 1) {
        const { nickname } = res.data.user_info;
        setMessage(nickname);
        Toast.show({ content: nickname });
        setTimeout(() => {
          history.push('/useDva');
        }, 1000);
      } else {
        Toast.show({ content: res.data.message });
      }
    },
  });

  // useRequest(() => service.useService.getUserInfo(info), {
  //   onSuccess: (res) => {
  //     if (res.rt === 1) {
  //       const { nickname } = res.data.user_info;
  //       setMessage(nickname);
  //       Toast.show({ content: nickname });
  //       setTimeout(() => {
  //         history.push('/useDva');
  //       }, 1000);
  //     } else {
  //       Toast.show({ content: res.data.message });
  //     }
  //   },
  // });

  // const run = async (info: Info) => {
  //   try {
  //     const res = await service.useService.getUserInfo(info);
  //     if (res.rt === 1) {
  //       const { nickname } = res.data.user_info;
  //       setMessage(nickname);
  //       Toast.show({ content: nickname });
  //       setTimeout(() => {
  //         history.push('/useDva');
  //       }, 1000);
  //     } else {
  //       Toast.show({ content: res.data.message });
  //     }
  //   } catch (error) {}
  // }

  return (
    <div className="sou-main-color" onClick={() => run(info)}>
      <h1 className={classNames(styles.h1, 'h1')}>click me to getUserInfo</h1>
      <UseComponents message={message}>
        <h3 className={styles.h1} />
      </UseComponents>
    </div>
  );
};

export default Home;
